<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="商机名称" prop="businessName">
                    <el-input class="w-[280px]" v-model="queryParams.businessName" />
                </el-form-item>
                <el-form-item label="对应客户" prop="privateCustomerCustomer">
                    <el-select
                        v-model="queryParams.privateCustomerCustomer"
                        class="w-[280px]"
                        clearable
                    >
                        <el-option label="张三" value="1" />
                        <el-option label="李四" value="2" />
                        <el-option label="王五" value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="预计签单日期" prop="estimatedSigningDate">
                    <daterange-picker
                        v-model:startTime="queryParams.createTimeStart"
                        v-model:endTime="queryParams.createTimeEnd"
                    />
                </el-form-item>
                <el-form-item label="签单可能性" prop="signingPossibility">
                    <el-select v-model="queryParams.signingPossibility" class="w-[280px]" clearable>
                        <el-option label="高" value="1" />
                        <el-option label="中" value="2" />
                        <el-option label="低" value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="业务人员" prop="fieldStaffId">
                    <el-select v-model="queryParams.fieldStaffId" class="w-[280px]" clearable>
                        <el-option label="张三" value="1" />
                        <el-option label="李四" value="2" />
                        <el-option label="王五" value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="协作人" prop="collaboratorId"
                    ><el-select v-model="queryParams.collaboratorId" class="w-[280px]" clearable>
                        <el-option label="张三" value="1" />
                        <el-option label="李四" value="2" />
                        <el-option label="王五" value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="创建人" prop="userId">
                    <el-select v-model="queryParams.userId" class="w-[280px]" clearable>
                        <el-option label="张三" value="1" />
                        <el-option label="李四" value="2" />
                        <el-option label="王五" value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <router-link
                    style="margin-right: 10px"
                    :to="{
                        path: getRoutePath('business:add')
                    }"
                >
                    <el-button v-perms="['business:add']" type="primary">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        新增
                    </el-button>
                </router-link>
                <el-button v-perms="['thread:add/edit']" type="primary">
                    <template #icon>
                        <icon name="el-icon-download" />
                    </template>
                    导出
                </el-button>
                <el-button v-perms="['thread:add/edit']" type="primary">
                    <template #icon>
                        <icon name="el-icon-upload" />
                    </template>
                    导入
                </el-button>
                <el-button v-perms="['thread:add/edit']" type="primary">
                    <template #icon>
                        <icon name="el-icon-Avatar" />
                    </template>
                    批量修改业务员
                </el-button>
                <el-button v-perms="['thread:add/edit']" type="primary">
                    <template #icon>
                        <icon name="el-icon-User" />
                    </template>
                    批量修改协作人
                </el-button>
            </div>
            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists" stripe>
                <el-table-column type="selection" width="55" />
                <el-table-column label="" prop="id" min-width="70" sortable />
                <el-table-column label="商机名称" prop="businessName" sortable min-width="130">
                    <template #default="{ row }">
                        <router-link
                            style="color: #409EFF"
                            :to="{
                                path: getRoutePath('business:check'),
                                query: {
                                    id: row.id,
                                    type: 'check'
                                }
                            }"
                        >
                            {{ row.businessName }}
                        </router-link>
                    </template></el-table-column
                >
                <el-table-column
                    sortable
                    label="对应客户"
                    prop="privateCustomerCustomer"
                    min-width="130"
                />
                <el-table-column
                    sortable
                    label="预计销售金额"
                    prop="estimatedSalesAmount"
                    min-width="150"
                />
                <el-table-column
                    sortable
                    label="预计签单日期"
                    prop="estimatedSigningDate"
                    min-width="180"
                />
                <el-table-column sortable label="所属部门" prop="department" min-width="130" />
                <el-table-column
                    sortable
                    label="签单可能性"
                    prop="signingPossibility"
                    min-width="130"
                />
                <el-table-column sortable label="业务人员" prop="fieldStaff" min-width="130" />
                <el-table-column sortable label="协作人" prop="collaborator" min-width="130" />
                <el-table-column sortable label="跟进状态" prop="followupStatus" min-width="130" />
                <el-table-column sortable label="下次跟进时间" prop="nextTime" min-width="180" />
                <el-table-column sortable label="操作人" prop="userName" min-width="130" />
                <el-table-column sortable label="创建时间" prop="createTime" min-width="180" />
                <el-table-column sortable label="更新时间" prop="updateTime" min-width="180" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['business:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            <router-link
                                :to="{
                                    path: getRoutePath('business:edit'),
                                    query: {
                                        id: row.id
                                    }
                                }"
                            >
                                编辑
                            </router-link>
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="business">
import { businessDelete, businessLists } from '@/api/business'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import { getRoutePath } from '@/router'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    businessName: '',
    privateCustomerCustomer: '',
    estimatedSigningDateStart: '',
    estimatedSigningDateEnd: '',
    signingPossibility: '',
    fieldStaffId: '',
    collaboratorId: '',
    userId: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: businessLists,
    params: queryParams
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await businessDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
